package cn.nfwx.tv.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nfwx.tv.R;
import cn.nfwx.tv.db.DownProgramModel;
import cn.nfwx.tv.helper.NetworkHelper;
import cn.nfwx.tv.util.GlideUtil;
import cn.nfwx.tv.util.SharePrefUtil;

/**
 * Created by zb.liu on 2017/11/1.
 */

public class DownLoadingAdapter extends BaseQuickAdapter<DownProgramModel, BaseViewHolder> {
    private boolean isDelete = false;
    private boolean isSelectAll = false;

    public void setDelete(boolean isDelete) {
        this.isDelete = isDelete;
    }

    public boolean isDelete() {
        return isDelete;
    }

    private HashMap<Integer, String> maps;
    private Map<Integer, Boolean> isFristMap;

    private OnCheckedChangeDelListener onCheckedChangeDelListener;

    public void setOnCheckedChangeDelListener(OnCheckedChangeDelListener onCheckedChangeDelListener) {
        this.onCheckedChangeDelListener = onCheckedChangeDelListener;
    }

    public interface OnCheckedChangeDelListener {
        // 是否全选中
        void onCheckedAllChanged(boolean stats);
    }

    public DownLoadingAdapter(int layoutResId, List data) {
        super(layoutResId, data);
        maps = new HashMap<Integer, String>();
        isFristMap = new HashMap<Integer, Boolean>();
        for (int i = 0; i < data.size(); i++) {
            isFristMap.put(i, false);
        }
    }

    public DownLoadingAdapter() {
        super(R.layout.fragment_downloading_item);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final DownProgramModel o) {
        final DownProgramModel item = o;
        ProgressBar progressBar = helper.getView(R.id.downloading_item_progressBar);
        helper.setText(R.id.downloading_item_name, item.getProgramName());
        String imagePath = item.getImagePath();
        if(TextUtils.isEmpty(imagePath)){
            imagePath = item.getLocalPath().replace(".mp4", ".jpg");
        }
        File file = new File(imagePath);
        if (file.exists()) {
            GlideUtil.loadSimpleImage(helper.itemView.getContext(), file.getAbsolutePath(), (ImageView) helper.getView(R.id.downloading_item_img));
        } else {
            GlideUtil.loadSimpleImage(helper.itemView.getContext(), item.getDownloadUrl().replace(".m3u8", ".jpg"), (ImageView) helper.getView(R.id.downloading_item_img));
        }
        helper.setVisible(R.id.downloading_item_progressBar, true);
        helper.setVisible(R.id.downloading_item_ondis, true);
        helper.setVisible(R.id.downloading_item_total, true);
        helper.setVisible(R.id.downloading_item_spilt, true);
        helper.setVisible(R.id.download_status, true);
        helper.setTextColor(R.id.downloading_item_precent, mContext.getResources().getColor(R.color.down_btn_bg));
        if (item.getStats() == DownProgramModel.STATS_DOWNLOADING) {
            long s1 = item.getDownloadLocation();
            long s2 = item.getSize();
            progressBar.setMax(item.getSize());
            helper.setText(R.id.downloading_item_ondis, s1 + "M");
            helper.setText(R.id.downloading_item_total, s2 + "M");
            progressBar.setProgress(item.getDownloadLocation());
            if (item.getDownloadLocation() == 0) {
                helper.setText(R.id.downloading_item_precent, "等待下载");
                helper.setImageResource(R.id.download_status, R.mipmap.mine_wait);
            } else {
                int percent = item.getDownloadLocation() * 100 / item.getSize();
                if (SharePrefUtil.getDownloadChoose(mContext)
                        && NetworkHelper.getInstance().isConnection4g(mContext)) {
                    helper.setText(R.id.downloading_item_precent, "运营商网络下已下载" + percent + "%");
                } else {
                    helper.setText(R.id.downloading_item_precent, "已下载" + percent + "%");
                }
                helper.setImageResource(R.id.download_status, R.mipmap.mine_downloading);
            }
        } else if (item.getStats() == DownProgramModel.STATS_DEFAULT) {
            long s1 = item.getDownloadLocation();
            long s2 = item.getSize();
            progressBar.setMax(item.getSize());
            helper.setText(R.id.downloading_item_ondis, s1 + "M");
            helper.setText(R.id.downloading_item_total, s2 + "M");
            progressBar.setProgress(item.getDownloadLocation());
            helper.setText(R.id.downloading_item_precent, "等待下载");
            helper.setImageResource(R.id.download_status, R.mipmap.mine_wait);
        } else if (item.getStats() == DownProgramModel.STATS_SUCCESS) {
            long s2 = item.getSize();
            helper.setVisible(R.id.downloading_item_progressBar, false);
            helper.setVisible(R.id.download_status, false);
            helper.setVisible(R.id.downloading_item_ondis, false);
            helper.setVisible(R.id.downloading_item_spilt, false);
            helper.setVisible(R.id.downloading_item_total, false);
            helper.setText(R.id.downloading_item_precent, s2 + "M");
            helper.setTextColor(R.id.downloading_item_precent, mContext.getResources().getColor(R.color.setting_lab_text));
        } else if (item.getStats() == DownProgramModel.STATS_PAUSE) {
            long s1 = item.getDownloadLocation();
            long s2 = item.getSize();
            progressBar.setMax(item.getSize());
            helper.setText(R.id.downloading_item_ondis, s1 + "M");
            helper.setText(R.id.downloading_item_total, s2 + "M");
            progressBar.setProgress(item.getDownloadLocation());
            if (!SharePrefUtil.getDownloadChoose(mContext)
                    && NetworkHelper.getInstance().isConnection4g(mContext)) {
                helper.setText(R.id.downloading_item_precent, "运营商网络下暂停");
            } else {
                helper.setText(R.id.downloading_item_precent, "下载暂停");
            }
            helper.setImageResource(R.id.download_status, R.mipmap.mine_pause);
        } else if (item.getStats() == DownProgramModel.STATS_WAIT) {
            long s1 = item.getDownloadLocation();
            long s2 = item.getSize();
            progressBar.setMax(item.getSize());
            helper.setText(R.id.downloading_item_ondis, s1 + "M");
            helper.setText(R.id.downloading_item_total, s2 + "M");
            progressBar.setProgress(item.getDownloadLocation());
            helper.setText(R.id.downloading_item_precent, "等待下载");
            helper.setImageResource(R.id.download_status, R.mipmap.mine_wait);
        }
        if (isDelete) {
            helper.setVisible(R.id.cb_downloading, true);
        } else {
            helper.setVisible(R.id.cb_downloading, false);
        }
        final CheckBox cb_downloading = helper.getView(R.id.cb_downloading);
        cb_downloading.setChecked(item.isCheck());
        cb_downloading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.setCheck(!item.isCheck());
                cb_downloading.setChecked(item.isCheck());
                if (onCheckedChangeDelListener != null) {
                    onCheckedChangeDelListener.onCheckedAllChanged(isDeleteAllDatas());
                }
            }
        });
        if (onCheckedChangeDelListener != null) {
            onCheckedChangeDelListener.onCheckedAllChanged(isDeleteAllDatas());
        }
    }

    public boolean isSelectAll() {
        return isSelectAll;
    }

    public void setSelectAll(boolean selectAll) {
        isSelectAll = selectAll;
        for (Object o : getData()) {
            if (o instanceof DownProgramModel) {
                DownProgramModel downProgramModel = (DownProgramModel) o;
                downProgramModel.setCheck(selectAll);
            }
        }
        notifyDataSetChanged();
    }

    /**
     * 获取删除列表
     *
     * @return
     */
    public List<DownProgramModel> getDeleteDatas() {
        List<DownProgramModel> downProgramModels = new ArrayList<>();
        for (Object o : getData()) {
            if (o instanceof DownProgramModel) {
                DownProgramModel item = (DownProgramModel) o;
                if (item.isCheck()) {
                    downProgramModels.add(item);
                }
            }
        }
        return downProgramModels;
    }


    public Map<Integer, String> getDeleteData() {
        return maps;
    }

    public void clearDeleteData() {
        maps.clear();
        isDelete = false;
    }


    public interface OnCheckedChangeDeleteListener {
        void onCheckedChanged(CompoundButton compoundButton, boolean b, int position);
    }

    /**
     * 获取删除的列表
     *
     * @return
     */
    public boolean isDeleteAllDatas() {
        boolean stats = true;
        for (Object o : getData()) {
            if (o instanceof DownProgramModel) {
                DownProgramModel item = (DownProgramModel) o;
                if (!item.isCheck()) {
                    stats = false;
                    break;
                }
            }
        }
        isSelectAll = stats;
        return stats;
    }
}
